<?php

class Content_CategoryController extends Zend_Controller_Action {

    public function indexAction() {
        $errors = $this->_getParam('error_handler');
        if ($errors && $errors instanceof ArrayObject && $errors->type === Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER) {
            return $this->_forward('error', 'error', 'default');
        }

        $url = trim($this->getRequest()->getPathInfo(), '/');
        $categories = new Content_Model_DbTable_Categories();
        $category = $categories->findByUrl($url);

        if (null === $category) {
            return $this->_forward('error', 'error', 'default');
        }

        $this->view->category = $category;
        $articles = new Content_Model_DbTable_Articles();
        switch ($category->template) {
            case 'single':
                $this->view->article = $articles->findSingleByCategory($category);
                break;
            case 'list':
                $this->view->articles = $articles->findListByCategory($category);
        }
        $this->_helper->viewRenderer($category->template);
    }

}
